<template>
  <div>
    <a-card class="card-header">
      <a-form-model :model="queryParam">
        <a-row :gutter="24">
          <a-col :span="5">
            <a-form-model-item
              label="入库单号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.acceptanceNum"
                placeholder="请输入入库单号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="5">
            <a-form-model-item
              label="加工单号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.processNum"
                placeholder="请输入加工单号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="5">
            <a-form-model-item
              label="商品编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.goodsCode"
                placeholder="请输入商品编号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="2">
            <a-button
              type="primary"
              style="margin-top:3px"
              @click="handleSearch"
              >查询</a-button
            >
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>

    <a-card class="card-content">
      <a-row :gutter="24">
        <a-col :span="22">
          <a-button type="primary" @click="handleAdd">新建入库</a-button>
          <!-- <a-button type="primary" @click="handleAdd">导出</a-button>
          <a-button type="primary" @click="handleAdd">导入</a-button> -->
        </a-col>
        <a-col :span="2">
          <a-button type="primary" @click="handleAllList('WarehousingList')"
            >全部入库单</a-button
          >
        </a-col>
      </a-row>
      <br />
      <a-table
        :columns="columns"
        :data-source="tableData"
        :loading="loading"
        :pagination="pagination"
        @change="handleTableChange"
        bordered
        :rowKey="(record, index) => record.id"
      >
        <template slot="shelfNames" slot-scope="text">
          <div style="text-align:left" v-if="text.length > 0">
            <div v-for="(item, index) in text" :key="index">
              {{ item }}
            </div>
          </div>
          <div v-else style="color:#c7254e">
            无库存
          </div>
        </template>
        <template slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" v-hasPromise="'wareDel'" />
          <a-popconfirm
            v-hasPromise="'wareDel'"
            okText="确定"
            cancelText="取消"
            title="确定删除吗?"
            @confirm="() => handleDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a-icon
            type="qrcode"
            @click.stop="handleCode(record)"
            style="fontSize: 16px;color: #40a9ff;"
          />
        </template>
      </a-table>
    </a-card>

    <!-- 二维码 -->
    <qrCode
      ref="qrCode"
      :label="qrModel.label"
      :text="qrModel.text"
      :qrSize="320"
    ></qrCode>

    <WarehousingModal ref="modalForm" @ok="modalFormOk"></WarehousingModal>
  </div>
</template>

<script>
import { columns } from "./components/tableConfig";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import WarehousingModal from "./components/WarehousingModal.vue";
import api from "@/api/apiServer";
import qrCode from "@/components/qrCode";
import { CodeNo } from "@/unit/QRCodeConfig";
export default {
  components: { WarehousingModal, qrCode },
  mixins: [tableMinxins],
  data() {
    return {
      columns,
      tableData: [],
      qrModel: {
        label: "", // 二维码底部内容
        text: "", // 二维码内容
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 生成二维码
    handleCode(record) {
      let { acceptanceNum } = record;
      let newObj = { acceptanceNum, QRCode: CodeNo.Warehousing };
      let text = JSON.stringify(newObj);
      this.$refs.qrCode.title = "入库二维码";
      this.qrModel.label = `${acceptanceNum}`;
      this.qrModel.text = text;
      this.$refs.qrCode.list = this.qrModel.label.split(",");
      this.$refs.qrCode.showModal();
    },
    // 获取列表数据
    async getList(arg) {
      //加载数据 若传入参数1则加载第一页的内容
      this.loading = true;
      if (arg === 1) {
        this.pagination.current = 1;
      }
      var params = this.getQueryParams();
      const res = await api.WarehousingPage(params);
      this.tableData = res.data.instocks;
      this.tableData.forEach((item) => {
        item.wareNum = 0;
        if (item.shelfNames != null) {
          item.shelfNames = item.shelfNames.split(","); //逗号切割并换行
        } else {
          item.shelfNames = [];
        }
        item.shelfIdAndGoodsQuantityVOList.forEach((each) => {
          item.wareNum += each.quantity;
        });
      });

      this.pagination.total = res.data.total;
      this.loading = false;
    },
    // 删除
    async handleDelete(record) {
      const ids = [];
      ids.push(record.id);
      const params = {
        ids,
      };
      const res = await api.WarehousingDelete(params);
      this.getList();
      this.$message.success(res.message);
    },
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
}
.card-content {
  button {
    margin-right: 20px;
  }
}
::v-deep .ant-form-item {
  margin-bottom: 0;
}
</style>
